package fei.yuzhi.com.loveflowers.utils;

import android.util.Log;

import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import fei.yuzhi.com.loveflowers.bean.evaluatebean.EvaluateBean;
import fei.yuzhi.com.loveflowers.bean.firstpageautobean.FirstAutoBean;
import fei.yuzhi.com.loveflowers.bean.firstpagedetailbean.FirstPageBaseDetailBean;
import fei.yuzhi.com.loveflowers.bean.searchbean.SearchBean;
import fei.yuzhi.com.loveflowers.configer.UrlConfiger;
import fei.yuzhi.com.loveflowers.fragment.firstpagefragment.FirstPageModle;
import fei.yuzhi.com.loveflowers.http.RetrofitService;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by My on 2016/10/6.
 */
public class FirstPageHttpUtils {


    public static void getMapFromNet(final onGetMapByNet getMapByNet){
        //利用retrofit进行网络访问
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(UrlConfiger.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Call<ResponseBody> call=retrofit.create(RetrofitService.class).getImageView();
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    //网络访问成功后得到json字符串
                    String jsonString=response.body().string();
                    //将得到的字符串传递到封装好的jiexi方法中，得到存放bean类集合的map集合
                    Map<String,List<?>> map=JieXi.jieXi(jsonString);
                    //通过自定义的接口可以将得到的map集合传递出去
                    getMapByNet.getMap(map);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }
    public interface onGetMapByNet{
        void getMap(Map<String,List<?>> map);
    }

    public static void getBaseDetailBean(String data, final OnGetBaseDetailBean getBaseDetailBean){
        Map<String,String> map=new HashMap<>();
        //act=goods&op=goods_detail&goods_id=103392&key=&version=4.3.0&client=android
        map.put("act","goods");
        map.put("op","goods_detail");
        map.put("goods_id",data);
        map.put("key","");
        map.put("version","4.3.0");
        map.put("client","android");
        Retrofit retrofit=new Retrofit.Builder().
                addConverterFactory(GsonConverterFactory.create()).
                baseUrl(UrlConfiger.BASE_URL).
                build();
        Call<FirstPageBaseDetailBean> call=retrofit.create(RetrofitService.class).getPageDetailBean(map);
        call.enqueue(new Callback<FirstPageBaseDetailBean>() {
            @Override
            public void onResponse(Call<FirstPageBaseDetailBean> call, Response<FirstPageBaseDetailBean>
                    response) {
                FirstPageBaseDetailBean baseDetailBean=response.body();
                getBaseDetailBean.getDataDetailBeanFromNet(baseDetailBean);
            }

            @Override
            public void onFailure(Call<FirstPageBaseDetailBean> call, Throwable t) {
            }
        });
    }
    public interface OnGetBaseDetailBean{
        void getDataDetailBeanFromNet(FirstPageBaseDetailBean baseDetailBean);
    }

    public static void getFirstAutoBean(String data, final onGetFirstAutoBean getFirstAutoBean){
        //act=index&op=special&version=4.3.0&special_id=362&type=html%20HTTP/1.1
        Map<String,String> map=new HashMap<>();
        map.put("act","index");
        map.put("op","special");
        map.put("version","4.3.0");
        map.put("special_id",data);
        map.put("type","html%20HTTP/1.1");
        Retrofit retrofit=new Retrofit.Builder().
                addConverterFactory(GsonConverterFactory.create()).
                baseUrl(UrlConfiger.BASE_URL).
                build();
        Call<FirstAutoBean> call=retrofit.create(RetrofitService.class).getFirstAutoBean(map);
        call.enqueue(new Callback<FirstAutoBean>() {
            @Override
            public void onResponse(Call<FirstAutoBean> call, Response<FirstAutoBean> response) {
                FirstAutoBean firstAutoBean=response.body();
                getFirstAutoBean.getFirstAutoBeanFromNet(firstAutoBean);
            }

            @Override
            public void onFailure(Call<FirstAutoBean> call, Throwable t) {

            }
        });
    }
    public interface onGetFirstAutoBean{
        void getFirstAutoBeanFromNet(FirstAutoBean firstAutoBean);
    }

    public static void getSearchBean(String data, final onSendSearchBean onSendSearchBean){
        Map<String,String> map=new HashMap<>();
        //act=goods&op=goods_list&curpage=1&page=10&keyword=&version=4.3.0&client=android
        map.put("act","goods");
        map.put("op","goods_list");
        map.put("curpage","1");
        map.put("page","10");
        map.put("keyword",data);
        map.put("version","4.3.0");
        map.put("client","android");
        Retrofit retrofit=new Retrofit.Builder().
                addConverterFactory(GsonConverterFactory.create()).
                baseUrl(UrlConfiger.BASE_URL).
                build();
        Call<SearchBean> call=retrofit.create(RetrofitService.class).getSearchBean(map);
        call.enqueue(new Callback<SearchBean>() {
            @Override
            public void onResponse(Call<SearchBean> call, Response<SearchBean> response) {
                SearchBean searchBean=response.body();
                onSendSearchBean.sendSearchBean(searchBean);
            }

            @Override
            public void onFailure(Call<SearchBean> call, Throwable t) {

            }
        });
    }
    public interface onSendSearchBean{
        void sendSearchBean(SearchBean searchBean);
    }

    public static void getEvaluateBean(String data, final onSendEvaluateBean onSendEvaluateBean){
        Map<String,String> map=new HashMap<>();
        //act=goods&op=comments&page=10&curpage=1&goods_id=103392&type=1&version=4.3.0&client=android
        map.put("act","goods");
        map.put("op","comments");
        map.put("page","10");
        map.put("curpage","1");
        map.put("goods_id",data);
        map.put("type","1");
        map.put("version","4.3.0");
        map.put("client","android");
        Retrofit retrofit=new Retrofit.Builder().
                addConverterFactory(GsonConverterFactory.create()).
                baseUrl(UrlConfiger.BASE_URL).
                build();
        Call<EvaluateBean> call=retrofit.create(RetrofitService.class).getEvaluateBean(map);
        call.enqueue(new Callback<EvaluateBean>() {
            @Override
            public void onResponse(Call<EvaluateBean> call, Response<EvaluateBean> response) {
                EvaluateBean evaluateBean=response.body();
                onSendEvaluateBean.sendEvaluateBean(evaluateBean);
            }

            @Override
            public void onFailure(Call<EvaluateBean> call, Throwable t) {

            }
        });
    }
    public interface onSendEvaluateBean{
        void sendEvaluateBean(EvaluateBean evaluateBean);
    }
}
